package org.example;

public class Test2 {
    //leetcode 108 将有序数组转换为二叉搜索树 https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/
    public TreeNode sortedArrayToBST(int[] nums) {
        return sortedArrayToBSTChild(nums,0,nums.length - 1);
    }

    public TreeNode sortedArrayToBSTChild(int[] nums, int left, int right) {
        if (left > right) return null;
        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.right = sortedArrayToBSTChild(nums,mid + 1,right);
        root.left = sortedArrayToBSTChild(nums,left,mid - 1);
        return root;
    }
}
